package com.bjpowernode.crm.setting.service.impl;

import com.bjpowernode.crm.exception.LoginException;
import com.bjpowernode.crm.setting.dao.UserDao;
import com.bjpowernode.crm.setting.domain.User;
import com.bjpowernode.crm.setting.service.UserService;
import com.bjpowernode.crm.utils.DateTimeUtil;
import com.bjpowernode.crm.utils.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    @Override
    public User login(String loginAct, String loginPwd, String ip) throws LoginException {

        Map<String, String> map = new HashMap<String, String>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);

        User user = userDao.login(map);

        if (user == null) {
            throw new LoginException("账号密码错误");
        }

        //如果程序能成功执行到该行,说明账号密码正确
        //需要继续向下验证信息

        //验证失效时间
        String expiredTime = user.getExpireTime();
        String currentTime = DateTimeUtil.getSysTime();
        if (expiredTime.compareTo(currentTime) < 0) {
            throw new LoginException("账号已失效");
        }

        //验证是否锁定
        String lockState = user.getLockState();
        if ("0".equals(lockState)) {
            throw new LoginException("账号已被锁定");
        }

        //验证ip地址是否被允许
        String allowIps = user.getAllowIps();

        if (!allowIps.contains(ip)) {
            throw new LoginException("ip地址受限");
        }

        return user;


    }

    @Override
    public List<User> getUserList() {

        return userDao.getUserList();
    }
}






























